package cn.xeblog.plugin.game.ngsnake.model;

import java.util.List;

/**
 * 蛇
 *
 * @author anlingyi
 * @date 2022/8/2 3:32 PM
 */
public class Snake {

  /**
   * 默认宽度
   */
  public static int width = 10;
  /**
   * 默认高度
   */
  public static int height = 10;

  /**
   * 蛇身坐标列表
   */
  public List<Point> body;

  /**
   * 构造
   *
   * @param body 蛇身坐标列表
   */
  public Snake(List<Point> body) {
    this.body = body;
  }

  /**
   * 添加蛇身坐标
   *
   * @param x 横坐标
   * @param y 纵坐标
   */
  public void add(int x, int y) {
    this.body.add(new Point(x * width, y * height));
  }

  /**
   * 移除蛇尾坐标
   */
  public void removeLast() {
    int size = this.size();
    if (size == 0) {
      return;
    }
    this.body.remove(size - 1);
  }

  /**
   * 获取蛇头坐标
   *
   * @return 蛇头坐标
   */
  public Point getHead() {
    return size() > 0 ? this.body.get(0) : null;
  }

  /**
   * 获取蛇尾坐标
   *
   * @return 蛇尾坐标
   */
  public Point getTail() {
    int size = size();
    if (size > 0) {
      return this.body.get(size - 1);
    }

    return null;
  }

  /**
   * 蛇身长度
   *
   * @return
   */
  public int size() {
    return this.body.size();
  }

  /**
   * 蛇移动
   *
   * @param direction 移动方向
   */
  public void move(Direction direction) {
    // 蛇没有长度，跳过
    if (size() == 0) {
      return;
    }

    for (int i = this.size() - 1; i > 0; i--) {
      // 从蛇尾开始向前移动
      Point point = this.body.get(i);
      Point nextPoint = this.body.get(i - 1);
      point.x = nextPoint.x;
      point.y = nextPoint.y;
    }

    // 蛇头移动
    Point head = getHead();
    switch (direction) {
      case UP:
        head.y -= height;
        break;
      case DOWN:
        head.y += height;
        break;
      case LEFT:
        head.x -= width;
        break;
      case RIGHT:
        head.x += width;
        break;
    }
  }

}
